{use master_page('admin/v_master')}
{block content} 
<?php 
$ci=&get_instance();
require_once(APPPATH."views/".$ci->template."/admin_helper.php");
$menu_infos = menus();
$menu_info = $menu_infos[$_GET['name']];
$ci->menu_info = $menu_info; 

function bl_menu_item($items,$level=1)
{
	$ci = & get_instance();
	// $menu_infos = menus();
	$menu_info = $ci->menu_info;
	
	foreach($items as $item)
	{
		$title = $item->title;
		$pad = "";
		for($i=0;$i<$level-1;$i++)
		{
			$pad = $pad . "----";
		}
		if($level == 1){$pad="+";} 
		// $title = str_pad($title,'-',strlen($title)+level,STR_PAD_LEFT);
		
		if($level>=$menu_info->max_level) return;
		echo "<option value='".$item->id."'>$pad$title</option>" ; 
		if(sizeof($item->items)>0) 
		{
			bl_menu_item($item->items,$level+1);
		}
	}
}

function build_options($menu_name)
{
	
}
?> 


<?php 
function bl_article_class($children,$level=1)
{
	foreach($children as $child)
	{
		$title = $child->title;
		$pad = "";
		if($level>1)
		{
			for($i=0;$i<$level-1;$i++)
			{
				$pad = $pad . "&nbsp;&nbsp;";
			}
			$pad = $pad."└";
		}
		if($level == 1){
			$pad="";
		}
		// $title = str_pad($title,'-',strlen($title)+level,STR_PAD_LEFT);

		echo "<option value='".$child->id."'>$pad$title</option>" ;
		if(sizeof($child->children)>0)
		{
			bl_article_class($child->children,$level+1);
		}
	}
}
?>
<?php 
function bl_page_class($children,$level=1)
{
	foreach($children as $child)
	{
		$title = $child->title;
		$pad = "";
		if($level>1)
		{
			for($i=0;$i<$level-1;$i++)
			{
				$pad = $pad . "&nbsp;&nbsp;";
			}
			$pad = $pad."└";
		}
		if($level == 1){
			$pad="";
		}
		// $title = str_pad($title,'-',strlen($title)+level,STR_PAD_LEFT);

		echo "<option value='".$child->id."'>$pad$title</option>" ;
		if(sizeof($child->children)>0)
		{
			bl_article_class($child->children,$level+1);
		}
	}
}
?>
<?php 
function bl_link_class($children,$level=1)
{
	foreach($children as $child)
	{
		$title = $child->title;
		$pad = "";
		if($level>1)
		{
			for($i=0;$i<$level-1;$i++)
			{
				$pad = $pad . "&nbsp;&nbsp;";
			}
			$pad = $pad."└";
		}
		if($level == 1){
			$pad="";
		}
		// $title = str_pad($title,'-',strlen($title)+level,STR_PAD_LEFT);

		echo "<option value='".$child->id."'>$pad$title</option>" ;
		if(sizeof($child->children)>0)
		{
			bl_article_class($child->children,$level+1);
		}
	}
}
?>

<script>
$(document).ready(function(){
	$("#type").change(function(){
		var url = "<?php echo _U("menu/edit?name=".$this->input->get('name')."&type=");?>";
		url = url + $(this).val();
		<?php if(isset($_GET['id'])){ echo "url = url + '&id=".$this->input->get('id')."';";}?>
		<?php if(isset($_GET['parent_id'])){ echo "url = url + '&parent_id=".$this->input->get('parent_id')."';";}?>
		//  alert(url) ; 
		window.location.href = url  ;     
	});
});
</script>

<form action="<?=$_SERVER['REQUEST_URI']?>" method="post" >
<div class="blank20" style="height:20px"></div>
<div id="tagscontent" class="right_box" >
	<table class="formtable" style="border:0px solid #cfcfcf;width:100%">
		<tr class='form_line'>
			<td class='title'><?=lang('menu_item_name')?></td>
			<td class='content'>
				<input name='p_name' size='20' value='<?=set_value('p_name', $name)?>' type='text' />  
				<?=form_error('p_name', '<font color="red">', '</font>')?>  
			</td>
		</tr>
		<tr class='form_line'>
			<td class='title' style="text-align:right;width:20%"><?=lang('menu_item_type')?></td>
			<td class='content' width="80%">
				<select id='type' class="bind" >
				<option value="">请选择</option> 
				<?foreach($menu_info->types as $name=>$value):?>
					<option value="{value}"><?=get_menu_type_name($value)?></option> 
				<?endforeach;?>
				</select>
				
				<?$type = !empty($_GET['type'])?$this->input->get('type'):$menu_item->type;?> 
				
				<input name='p_type' size='30' value='<?=set_value('p_type',$type)?>' type='hidden' />  
				
				<?=form_error('p_type', '<font color="red">', '</font>')?>  
				
				<?if(!empty($type)&& $type!="guest_book"):?>
				<select id='key_id' class="bind">
				<option value="">请选择</option> 
				<?
					$resources = Array();
					if($type == Menu_Item_Type::Page)
					{
						$resources = $this->page_manager->get_list();
					}
					if($type == Menu_Item_Type::Link)
					{
						$resources = $this->link_manager->get_list();
					}
					if($type == Menu_Item_Type::Article_Class)
 					{
						$resources = $this->article_class_manager->get_root_list(); 
					}
					if($type == Menu_Item_Type::Page_Class)
 					{
						$resources = $this->page_class_manager->get_root_list(); 
					}
					if($type == Menu_Item_Type::Link_Class)
 					{
						$resources = $this->link_class_manager->get_root_list(); 
					}
					/*
					if($type == Menu_Item_Type::Guest_Book)
					{
						$resources = $this->guest_book_manager->get_root_list();
					}*/
				?>
				<?foreach($resources as $resource):?>
					<option value="{resource->id}">{resource->title}</option> 
					<?php if($type == Menu_Item_Type::Article_Class)
					{
						bl_article_class($resource->children,2);
					}?>
					<?php if($type == Menu_Item_Type::Page_Class)
					{
						bl_page_class($resource->children,2);
					}?>
					<?php if($type == Menu_Item_Type::Link_Class)
					{
						bl_link_class($resource->children,2);
					}?>
				<?endforeach;?>
				</select>
				<? if($type== Menu_Item_Type::Page || $type== Menu_Item_Type::Link || $type == Menu_Item_Type::Article_Class || $type == Menu_Item_Type::Page_Class || $type == Menu_Item_Type::Link_Class):?>
				<?php //print_r(_CU());exit;?>
				<a href="<?=_U("$type/edit?returnurl=".urlencode(_CU()))?>">新增</a>
				<span id="btn_edit"></span>
				<? endif;?>
				
				<input name='p_key_id' value='<?=set_value('p_key_id',!empty($_GET['key_id'])?$this->input->get('key_id'):$key_id)?>' type='hidden' />  
				<?=form_error('p_key_id', '<font color="red">', '</font>')?>  
				<script>
				$(document).ready(function(){
					$('#key_id').change(function(){
						var key_id = $(this).val();
						if("<?=$type?>"!='123')
						{
							if(key_id == '')
							{
								$('#btn_edit').html("");  
							}
							else
							{
								$('#btn_edit').html("<a href='<?=_U("$type/edit?returnurl=".urlencode(_CU())."&id=")?>" + key_id + "'>编辑</a>");
							}
						}
					});
					$('#key_id').change();
				});
				</script>
				
				<?else:?>
				<input name='p_key_id' value='0' type='hidden' />  
				<?endif;?>
			</td>
		</tr>
		<!--tr class='form_line'>
			<td class='title'><?=lang('menu_item_key')?></td>
			<td class='content'>
				<input name='p_key' size='30' value='<?=set_value('p_key',$key)?>' type='text' />  
				<?=form_error('p_key', '<font color="red">', '</font>')?>  
			</td> 
		</tr-->  
		<tr class='form_line'>
			<td class='title'><?=lang('menu_item_target_window')?></td>
			<td class='content'>
				<select id="target_window" class='bind'>
					<option value=''>请选择</option> 
					<option value='_self'>本窗口</option>
					<option value='_blank'>新窗口</option>
					<option value='_parent'>父窗口</option> 
					<option value='custom'>自定义</option>
				</select>
				
				<input name='p_target_window' style="display:none" size='6' value='<?=set_value('p_target_window', $target_window)?>' type="text" />  
				<?=form_error('p_target_window', '<font color="red">', '</font>')?>  
			</td>
		</tr> 
		<tr class='form_line'>
			<td class='title'><?=lang('menu_item_parent')?></td>  
			<td class='content'>
				
				<select id='parent_id' class="bind">
					<option value="0">请选择</option> 
					<?php
					/*
					$menu_item1 = $menu_item;
					if(isset($_GET['id']))
					{
						$menu_item1 = new menu_item_model();
					}*/
					$tmp_menu = $this->menu_manager->get($_GET['name']);
					?>
					<?php if(sizeof($tmp_menu->items)>0){bl_menu_item($tmp_menu->items,1);}?>     
				</select>
				<input name='p_parent_id' size='2' value='<?=set_value('p_parent_id',($parent_id=='')?(isset($_GET['parent_id'])?$_GET['parent_id']:0):$parent_id)?>' type='hidden' />  
				<?=form_error('p_parent_id', '<font color="red">', '</font>')?>  
			</td>
		</tr>
		<tr class='form_line' style="display:none">
			<td class='title'><?=lang('menu_item_order')?></td>  
			<td class='content'>
				<input style="width:138px" name='p_order' size='2' value='<?=set_value('p_order',empty($order)?'0':$order)?>' type='text' />  
				<?=form_error('p_order', '<font color="red">', '</font>')?>  
			</td>
		</tr>
	</table> 
	<div class="blank20"style="height:20px"></div>
	<input name ="p_menu_name" value="<?php echo $this->input->get('name');?>" type="hidden"/>	
	<input type="submit" name="submit" value=" 提交 " class="btn_a">
</div>
<!--div class="fromtable_right" style="float:left">fff</div  -->









</form> 
{end}